﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;

namespace aucn.web.au.code
{
    public class Facility
    {
        private string _facId, _facName, _facDesc;
        private List<ImageWithDescription> _imgList;

        public string FacilityID { get { return this._facId; }  }
        public string FacilityName { get { return this._facName; } }
        public string FacilityDescription { get { return this._facDesc; } }
        public List<ImageWithDescription> ImageList { get { return this._imgList; } }

        public Facility(string facId, string facName, string facDesc)
        {
            this._facId = facId;
            this._facName = facName;
            this._facDesc = facDesc;
            this._imgList = new List<ImageWithDescription>();
        }

        public static Facility FromXml(string facilityId)
        {
            facilityId = facilityId.Trim().ToUpper();
            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("~/App_Data/facility.xml"));
            XmlNamespaceManager xm = new XmlNamespaceManager(doc.NameTable);
            xm.AddNamespace("x", "http://tempuri.org/facility.xsd");

            XmlNode facNode = doc.DocumentElement.SelectSingleNode("x:Facility[@Name='" + facilityId + "']", xm);
            Facility ret = null;
            
            if (facNode != null)
            {
                string name, desc;
                try
                {
                    name = facNode.SelectSingleNode("x:FacilityName", xm).InnerText;
                }
                catch{name=string.Empty;}

                try
                {
                    desc = facNode.SelectSingleNode("x:Description", xm).InnerText;
                }
                catch { desc = string.Empty; }

                ret = new Facility(facilityId, name, desc);

                XmlNodeList imgNodeList = facNode.SelectNodes("x:ImageList/x:Image", xm);
                foreach (XmlNode imgNode in imgNodeList)
                {
                    ImageWithDescription img = new ImageWithDescription();
                    
                    img.ImageUrl = imgNode.SelectSingleNode("x:ImageUrl", xm).InnerText;
                    try
                    {
                        img.Caption = imgNode.SelectSingleNode("x:AltText", xm).InnerText;
                    }
                    catch { img.Caption = string.Empty; }
                    
                    img.AlternateText = img.Caption;
                    try
                    {
                        img.Description = imgNode.SelectSingleNode("x:ImageDesc", xm).InnerText;
                    }
                    catch { img.Description = string.Empty; }
                    ret.ImageList.Add(img);
                }
            }

            return ret;
        }
    }
}